﻿$(window).ajaxSend(function(evt, request, settings){
	$(".loader").show();
}).ajaxComplete(function(event,request, settings){
	$(".loader").hide();
});

/**
 * 设置title
 * @param title
 * @return null
 */
function setTitle(title){
	document.title = title;
}

/**
 * 数据请求方法
 * @param requestData
 * @param httpHeader
 * @param responseFn
 * @return
 */
function dataConn(requestData,httpHeader,responseFn){
	$.ajax({
		type:"post",
		url:_URL,
		dataType:"json",
		data:requestData,
		beforeSend:function(XMLHttpRequest){
			XMLHttpRequest.setRequestHeader("cmd",httpHeader);
		},
		success:function(data,textStatus){
			if(data.httpstatus==302){
				popError("服务器又开小差啦！请稍后再试吧！","302错误");
				return false;
			}
			if(responseFn!=undefined){
				eval(responseFn(data,textStatus));
			}
		},
		error:function(){
			popError("请求出错！请稍后再试吧！","错误请求");
		}
	});
}

/**
 * 同步加载页面
 * @param {String} container 页面存放的div容器
 * @param {String} url 加载的页面URL
 * @param {String} callback 回调函数
 */
function loadPage(container,url,callback) {
	$.ajax({
		url : url,
		async : false,
		success : function(html) {
			$(container).html(html);
		},
		complete : function() {
			if(callback!=undefined){
				eval(callback);
			}
		},
		error : function(XmlHttpRequest, textStatus, errorThrown) {
			popError("加载页面出错，请进行刷新，然后再试一次。");
		}
	});
}

/**
 * 动态加载js和css
 * @param file 文件路径+文件名称
 */
function includeJs(file){
	var js = document.createElement('script');
	js.type = 'text/javascript';
	js.src = file;
	if(js.readyState){
		js.onreadystatechange = function(){
			if (js.readyState == "loaded" || js.readyState == "complete"){
				document.getElementsByTagName('head')[0].appendChild(js);
			}
		};
	}else{
	    document.getElementsByTagName('head')[0].appendChild(js);
	}
}
function includeCss(file) {
	css=document.createElement('link'); 
	css.href=file; 
	css.rel='stylesheet'; 
	css.type='text/css';
	document.getElementsByTagName('head').item(0).appendChild(css);
}

/**
 * 字符串查找
 * @param object 查找的字符
 * @param str 被查找的字符串
 * @return Boolean
 */
function hasString(object,str){
	var reg=eval("/"+object+"/gi");
	if(reg.test(str)){
		return true;
	}else{
		return false;
	}
}

/**
 * 提示信息
 */
var showInfoTimer=null;
function showInfo(text){
	clearTimeout(showInfoTimer);
	$("#infoPanel").fadeIn().children("span").removeClass("errorInfo").html(text);
	showInfoTimer=setTimeout(function(){
		$("#infoPanel").fadeOut();
	},3000);
}
function showErrorInfo(text){
	showInfo(text);
	$("#infoPanel span").addClass("errorInfo");
}

/**
 * 分页
 * @param id 插入页码的地方
 * @param hashArray hash数组值
 * @param count 总数
 * @return
 */
function setPager(id,pageSize,hashArray,count){
	var theHtml="",className="";
	if(count==0 || count==undefined){
		$("#goodsPager").html(theHtml);
		return false;
	}
	var currentPager=1,pageStep=5,pageCount,beginPage,endPage;
	//分页算法
	pageCount=Math.ceil(count/pageSize);
	currentPager=parseInt(hashArray[2]?hashArray[2]:1);
	if(pageCount<currentPager){currentPager=pageCount;}
	if(currentPager<1){currentPager=1;}
	beginPage=(currentPager-Math.ceil(pageStep/2));
	if(beginPage<1){beginPage=1;}
	endPage=currentPager+pageStep/2;
	if(endPage>pageCount){endPage=pageCount+1;}
	
	if(currentPager==1){
		theHtml+='<a href="javascript:" class="disabled">上一页</a>';
	}else{
		theHtml+='<a href="'+hashArray[0]+'_'+hashArray[1]+'_'+(currentPager-1)+'">上一页</a>';
	}
	for(var i=beginPage;i<endPage;i++){
		if(currentPager==i){
			className="selected";
		}else{
			className="";
		}
		theHtml+='<a href="'+hashArray[0]+'_'+hashArray[1]+'_'+i+'" class="'+className+'">'+(i)+'</a>';
	}
	if(currentPager==pageCount){
		theHtml+='<a href="javascript:" class="disabled">下一页</a>';
	}else{
		theHtml+='<a href="'+hashArray[0]+'_'+hashArray[1]+'_'+(currentPager+1)+'">下一页</a>';
	}
	theHtml+='<span class="pager_total">共<span id="pagerTotal">'+pageCount+'</span>页</span><span class="goto_pager">到</span><input type="text" class="text" id="gotoPage" /><span class="goto_pager">页</span><a href="javascript:" id="gotoSubset" class="a_as_button">确定</a>';
	$(id).html(theHtml);
	$("#gotoPage").blur(function(){
		var i=$(this).val();
		if(isNaN(i)){i=1;}
		if(i>pageCount){i=pageCount;}
		if(i!=""){
			$("#gotoSubset").attr("href",hashArray[0]+'_'+hashArray[1]+'_'+i);
		}
	});
}

/**
 * 生成id
 * @return
 */
function generateGuid(){
    var guid = "";
    for (var i = 1; i <= 32; i++){
          var n = Math.floor(Math.random()*16.0).toString(16);
          guid += n;
//          if((i==8)||(i==12)||(i==16)||(i==20))
//              guid += "-";
    }
    return guid;
}
/**
 * 获取指定的URL参数值 
 * @param {String} paramName URL参数
 * @return paramValue URL参数值
 */
function getParam(paramName) {
	var paramValue = "";
	var isFound = false;
	if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
		var arrSource = unescape(this.location.search).substring(1,this.location.search.length).split("&");
		i = 0;
		while (i < arrSource.length && !isFound) {
			if (arrSource[i].indexOf("=") > 0) {
				if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
					paramValue = arrSource[i].split("=")[1];
					isFound = true;
				}
			}
			i++;
		}
	}
	return paramValue;
}

/**
 * 通过name获取对象
 * @param {id} movieName
 * @return
 */
function getByName(movieName){
  if (window.document[movieName])  {
  	return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1){
  	if (document.embeds && document.embeds[movieName])
  	return document.embeds[movieName];  
  }else{ // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  	return document.getElementById(movieName);
  } 
}

/**
 * 弹出操作层 popDiv('url');return false;
 * @loadUrl 加载到弹出层作为内容的链接地址
 * @hash 给关闭的标签添加前页面的hash
 * @.pop_close 取消按钮，直接执行clearPop();清除弹出层
 */
function popDiv(loadUrl,hash,callback){
	$.ajax({
	  url: loadUrl,
	  cache: false,
	  async : false,
	  error: function(XmlHttpRequest, textStatus, errorThrown){
	  	popError("加载页面出错，请进行刷新，然后再试一次。","发生错误"); 
	  },
	  success: function(html){
	  	dialogHtml(html,"",hash);
	  },
	  complete: function(){
			positionDiv();
			if(callback!=undefined){
	  		eval(callback);
	  	}
	  }
	});
}

/**
 * 弹出层html的添加
 */
var dialogHtml = function(html,title,hash){
	clearPop();
	if(title!=null && title!=""){
		html="<div class='pop_content_title'>"+title+"</div><div class='pop_content_container'>"+html+"</div>";
	}
	hash!=undefined ? hash="#"+hash : hash="javascript:clearPop();";
	var popHtml=
		"<a href='"+hash+"' class='pop_bg'></a>" +
		"<table class='pop_dialog_table'>" +
		"	<tbody>" +
		"		<tr><td class='pop_topleft'></td><td class='pop_border'></td><td class='pop_topright'></td></tr>" +
		"		<tr><td class='pop_border'></td><td>" +
		"			<div class='pop_content_box'>" +
		"				<div><a href='"+hash+"' class='pop_close'>×</a></div>" +
		"				<div class='pop_content'>"+html+"</div>" +
		"			</div>" +
		"		</td><td class='pop_border'></td></tr>" +
		"		<tr><td class='pop_bottomleft'></td><td class='pop_border'></td><td class='pop_bottomright'></td></tr>" +
		"	</tbody>" +
		"</table>"
	$("body").append(popHtml);
	if($.browser.msie && $.browser.version == "6.0"){
		$(window).scroll(function() {
			positionDiv();
		});
	}
	$(window).resize(function(){
		positionDiv();
	});
}

/**
 * 弹出层的位置重新设置
 */
function positionDiv() {
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var thisWidth,thisHeight,topOffset,lefOffset,scrollTop=0;
	thisWidth = parseFloat($(".pop_dialog_table").width());
	thisHeight = parseFloat($(".pop_dialog_table").height());
	if($.browser.msie && $.browser.version == "6.0"){
		scrollTop=$(document).scrollTop();
	}
	$(".pop_dialog_table").css({top:windowHeight/2-thisHeight/2+scrollTop,left:windowWidth/2-thisWidth/2});
};

/**
 * 清除弹出层方法
 */
function clearPop() {
	$("table.pop_dialog_table,.pop_bg").remove();
}

/**
 * 弹出提示层 popRight('content');popError('content');
 * 继承popTips
 */
function popError(content,title){
	if(title==null){
		title="错误";
	}
	popDialog(content,title);
}
function popRight(content,title){
	if(title==null){
		title="正确";
	}
	popDialog(content,title);
}
function popDialog(content,title){
	dialogHtml(content,title);
	positionDiv();
}
